package com.example.onlinemonitoring.serviceImpl;

import com.example.onlinemonitoring.model.Scsensor;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Scbussensor;
import com.example.onlinemonitoring.model.Schistory;
import com.example.onlinemonitoring.model.Sclinesensor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.dao.SclinesensorMapper;
import com.example.onlinemonitoring.service.ScsensorService;

import tk.mybatis.mapper.entity.Example;

import com.example.onlinemonitoring.model.common.alarmmessage;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.Util.HexUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description Scsensor接口实现
* @author  generator
* @since  2022-04-09 13:03:09
*/
@Service("scServiceImpl")
public class ScsensorServiceImpl extends SensorAbstractBaseService<Scsensor, Schistory> implements ScsensorService {
    @Autowired
    SclinesensorServiceImpl sclinesensorserviceimpl;
    @Autowired
    ScbussensorServiceImpl scbussensorserviceimpl;
    public List<alarmmessage> getAlarmmessageList(Appdevice ad) {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<alarmmessage> list = new ArrayList<>();
        List<Scsensor> sensorlist = selectAll();
        for (int i = 0; i < sensorlist.size(); i++) {
            if(sensorlist.get(i).getLasttotalm() == 1){
                alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                sensorlist.get(i).getDevicegroup(),
                sensorlist.get(i).getDeviceaddr(),
                0,
                ad.getName(),
                sensorlist.get(i).getPointname(),
                sensorlist.get(i).getLastcommunicationstatus(),
                sdf.format(sensorlist.get(i).getLastdatatime()),
                sensorlist.get(i).getLasttotalm() == 1?2:0,
                sensorlist.get(i).getLasttotalm() == 1?"总告警":"");
                list.add(am);
            }
            if(sensorlist.get(i).getLastgrdtotalm() == 1){
                alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                sensorlist.get(i).getDevicegroup(),
                sensorlist.get(i).getDeviceaddr(),
                0,
                ad.getName(),
                sensorlist.get(i).getPointname(),
                sensorlist.get(i).getLastcommunicationstatus(),
                sdf.format(sensorlist.get(i).getLastdatatime()),
                sensorlist.get(i).getLastgrdtotalm() == 1?2:0,
                sensorlist.get(i).getLastgrdtotalm() == 1?"接地总告警":"");
                list.add(am);
            }
            if(sensorlist.get(i).getLasttriptotalm() == 1){
                alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                sensorlist.get(i).getDevicegroup(),
                sensorlist.get(i).getDeviceaddr(),
                0,
                ad.getName(),
                sensorlist.get(i).getPointname(),
                sensorlist.get(i).getLastcommunicationstatus(),
                sdf.format(sensorlist.get(i).getLastdatatime()),
                sensorlist.get(i).getLasttriptotalm() == 1?2:0,
                sensorlist.get(i).getLasttriptotalm() == 1?"跳闸总告警":"");
                list.add(am);
            }
            if(sensorlist.get(i).getLastresonanttotalm() == 1){
                alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                sensorlist.get(i).getDevicegroup(),
                sensorlist.get(i).getDeviceaddr(),
                0,
                ad.getName(),
                sensorlist.get(i).getPointname(),
                sensorlist.get(i).getLastcommunicationstatus(),
                sdf.format(sensorlist.get(i).getLastdatatime()),
                sensorlist.get(i).getLastresonanttotalm() == 1?2:0,
                sensorlist.get(i).getLastresonanttotalm() == 1?"谐振总告警":"");
                list.add(am);
            }
            if(sensorlist.get(i).getLastdeviceabnormalalm() == 1){
                alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                sensorlist.get(i).getDevicegroup(),
                sensorlist.get(i).getDeviceaddr(),
                0,
                ad.getName(),
                sensorlist.get(i).getPointname(),
                sensorlist.get(i).getLastcommunicationstatus(),
                sdf.format(sensorlist.get(i).getLastdatatime()),
                sensorlist.get(i).getLastdeviceabnormalalm() == 1?2:0,
                sensorlist.get(i).getLastdeviceabnormalalm() == 1?"装置异常告警":"");
                list.add(am);
            }
        }
        list.addAll(getAlarmmessageListByBusClass(ad,Scbussensor.class));
        list.addAll(getAlarmmessageListByLineClass(ad,Sclinesensor.class));
        return list;
    }
    public List<alarmmessage> getAlarmmessageListByBusClass(Appdevice ad,Class<Scbussensor> t) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<alarmmessage> list = new ArrayList<>();
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid",ad.getStationid());
        criteria.andEqualTo("devicegroup",ad.getDevicegroup());
        criteria.andEqualTo("inuse",1);
        criteria.andNotEqualTo("lastalarmlevel",0);
        List<Scbussensor> sensorlist = scbussensorserviceimpl.selectByExample(example);
        for (int i = 0; i < sensorlist.size(); i++) {
            String lastcommunicationstatus = getValue("lastcommunicationstatus",sensorlist.get(i));
            String pointaddr = getValue("pointaddr",sensorlist.get(i));
            String lastalarmlevel = getValue("lastalarmlevel",sensorlist.get(i));
            String lastalarmmessage = getValue("lastalarmmessage",sensorlist.get(i));
            alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                    sensorlist.get(i).getDevicegroup(),
                    sensorlist.get(i).getDeviceaddr(),
                    Integer.valueOf(HexUtil.isnullToInteger(pointaddr)),
                    ad.getName(),
                    sensorlist.get(i).getPointname(),
                    Integer.valueOf(HexUtil.isnullToInteger(lastcommunicationstatus)),
                    sdf.format(new Date()),
                    Integer.valueOf(HexUtil.isnullToInteger(lastalarmlevel)),
                    HexUtil.isnull(lastalarmmessage));
            list.add(am);
        }
        return list;
    }
    public List<alarmmessage> getAlarmmessageListByLineClass(Appdevice ad,Class<Sclinesensor> t) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<alarmmessage> list = new ArrayList<>();
        Example example = new Example(t);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid",ad.getStationid());
        criteria.andEqualTo("devicegroup",ad.getDevicegroup());
        criteria.andEqualTo("inuse",1);
        criteria.andNotEqualTo("lastalarmlevel",0);
        List<Sclinesensor> sensorlist = sclinesensorserviceimpl.selectByExample(example);
        for (int i = 0; i < sensorlist.size(); i++) {
            String lastcommunicationstatus = getValue("lastcommunicationstatus",sensorlist.get(i));
            String pointaddr = getValue("pointaddr",sensorlist.get(i));
            String lastalarmlevel = getValue("lastalarmlevel",sensorlist.get(i));
            String lastalarmmessage = getValue("lastalarmmessage",sensorlist.get(i));
            alarmmessage am = new alarmmessage(sensorlist.get(i).getStationid(),
                    sensorlist.get(i).getDevicegroup(),
                    sensorlist.get(i).getDeviceaddr(),
                    Integer.valueOf(HexUtil.isnullToInteger(pointaddr)),
                    ad.getName(),
                    sensorlist.get(i).getPointname(),
                    Integer.valueOf(HexUtil.isnullToInteger(lastcommunicationstatus)),
                    sdf.format(new Date()),
                    Integer.valueOf(HexUtil.isnullToInteger(lastalarmlevel)),
                    HexUtil.isnull(lastalarmmessage));
            list.add(am);
        }
        return list;
    }
    public int getAlarmCount(int stationid, int deviceaddr) {
        List<Scsensor> sensorlist = selectAll();
        int index = 0;
        for (int i = 0; i < sensorlist.size(); i++) {
            if(sensorlist.get(i).getLasttotalm() == 1){
                index++;
            }
            if(sensorlist.get(i).getLastgrdtotalm() == 1){
                index++;
            }
            if(sensorlist.get(i).getLasttriptotalm() == 1){
                index++;
            }
            if(sensorlist.get(i).getLastresonanttotalm() == 1){
                index++;
            }
            if(sensorlist.get(i).getLastdeviceabnormalalm() == 1){
                index++;
            }
        }
        return index;
    }
    public int getWarnCount(int stationid, int deviceaddr) {
        return 0;
    }

    public int Delete(Appdevice record){
        sclinesensorserviceimpl.Delete(record);
        scbussensorserviceimpl.Delete(record);
        return super.Delete(record);
    }
    public int Add(Appdevice record){
        int ret = 1;
        for (int i = record.getAddrmin(); i < record.getAddrmax() + 1; i++) {
            Example example = new Example(Scsensor.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid",record.getStationid());
            criteria.andEqualTo("devicegroup",record.getDevicegroup());
            criteria.andEqualTo("deviceaddr",i);
            for (int j = 1; j <= 1; j++) {
                criteria.andEqualTo("pointaddr",j);
                Scsensor sensor = new Scsensor();
                sensor.setAddrname(i + "#主机");
                sensor.setPointname(i+"-"+j);
                sensor.setDeviceaddr(i);
                sensor.setPointaddr(j);
                sensor.setStationid(record.getStationid());
                sensor.setDevicegroup(record.getDevicegroup());
                sensor.setLastcommunicationstatus(-1);
                sensor.setLastdatatime(new Date());
                sensor.setLastalarmlevel(0);
                sensor.setLastalarmmessage("");
                sensor.setMemo("");
                sensor.setInuse(1);
                if(selectOneByExample(example)==null){
                    if (insertSelective(sensor) == 0) {
                        ret = 0;
                    }
                }
            }
        }
        for (int i = record.getAddrmin(); i < record.getAddrmax() + 1; i++) {
            Example example = new Example(Scsensor.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid",record.getStationid());
            criteria.andEqualTo("devicegroup",record.getDevicegroup());
            criteria.andEqualTo("deviceaddr",i);
            for (int j = 1; j <= 48; j++) {
                criteria.andEqualTo("pointaddr",j);
                Sclinesensor sensor = new Sclinesensor();
                sensor.setAddrname(i + "#主机");
                sensor.setPointname(i+"-"+j);
                sensor.setDeviceaddr(i);
                sensor.setPointaddr(j);
                sensor.setStationid(record.getStationid());
                sensor.setDevicegroup(record.getDevicegroup());
                sensor.setLastcommunicationstatus(-1);
                sensor.setLastdatatime(new Date());
                sensor.setLastalarmlevel(0);
                sensor.setLastalarmmessage("");
                sensor.setMemo("");
                sensor.setInuse(1);
                if(sclinesensorserviceimpl.selectOneByExample(example)==null){
                    if (sclinesensorserviceimpl.insertSelective(sensor) == 0) {
                        ret = 0;
                    }
                }
            }
        }
        for (int i = record.getAddrmin(); i < record.getAddrmax() + 1; i++) {
            Example example = new Example(Scsensor.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stationid",record.getStationid());
            criteria.andEqualTo("devicegroup",record.getDevicegroup());
            criteria.andEqualTo("deviceaddr",i);
            for (int j = 1; j <= 4; j++) {
                criteria.andEqualTo("pointaddr",j);
                Scbussensor sensor = new Scbussensor();
                sensor.setAddrname(i + "#主机");
                sensor.setPointname(i+"-"+j);
                sensor.setDeviceaddr(i);
                sensor.setPointaddr(j);
                sensor.setStationid(record.getStationid());
                sensor.setDevicegroup(record.getDevicegroup());
                sensor.setLastcommunicationstatus(-1);
                sensor.setLastdatatime(new Date());
                sensor.setLastalarmlevel(0);
                sensor.setLastalarmmessage("");
                sensor.setMemo("");
                sensor.setInuse(1);
                if(scbussensorserviceimpl.selectOneByExample(example)==null){
                    if (scbussensorserviceimpl.insertSelective(sensor) == 0) {
                        ret = 0;
                    }
                }
            }
        }
        return ret;
    }
}
